package com.znsd.exam.controllor;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import com.znsd.entitys.ExamHistoryInfo;
import com.znsd.entitys.ExamPlan;
import com.znsd.entitys.Users;
import com.znsd.exam.service.ExamHistoryPaperService;
import com.znsd.exam.service.ExamPlanService;
import com.znsd.exam.service.UserServicePan;

@Controller
public class ExamHistoryControllorPan {
	@Autowired
	private ExamPlanService examPlanService;
	@Autowired
	private UserServicePan userServicePan;
	@Autowired
	private ExamHistoryPaperService examHistoryPaperService;
	
	@RequestMapping(value = "/selectAchievement/{examPaperId}",method = RequestMethod.GET)
	public String selectAchievement(@PathVariable("examPaperId") Integer examPaperId,HttpServletRequest request) {
		HttpSession session = request.getSession();
		session.setAttribute("examPaperId", examPaperId);
		return "admin/achievement";
	}
	@ResponseBody
	@RequestMapping("/selectAchievement/getAchievement")
	public Map<String, Object> getAchievement(@RequestParam("page") Integer page,@RequestParam("limit") Integer limit,HttpServletRequest request){
		HttpSession session = request.getSession();
		Integer examPaperId= (Integer) session.getAttribute("examPaperId");
		Map<String, Object> map=new HashMap<String, Object>();
		List<ExamPlan> list = examPlanService.getOneClass(examPaperId);
		if(list.size() == 1) {
			for (ExamPlan ep : list) {
				if (ep.getClazz().getClazz_id()!=null) {
					List<Users> stu = userServicePan.getClassStudent(ep.getClazz().getClazz_id());
					for (Users user : stu) {
						Map<String, Integer> m=new HashMap<String, Integer>();
						m.put("studentId", user.getUser_id());
						m.put("examPaperId", examPaperId);
						ExamHistoryInfo xhi = examHistoryPaperService.getUserCheck(m);
						if(xhi == null) {
							 ExamHistoryInfo ex=new ExamHistoryInfo(); ex.setStudentId(user.getUser_id());
							 ex.setExamPaperId(examPaperId);
							 ex.setExamScore(0); 
							 examHistoryPaperService.save(ex);
						}
					}
					List<Users> li = userServicePan.getAchievement(ep.getClazz().getClazz_id());
					List<Map<String, Object>> lis=new ArrayList<Map<String,Object>>();
					for (Users user : li) {
						Map<String, Object> m=new HashMap<String, Object>();
						m.put("user_id", user.getUser_id());
						m.put("user_sex", user.getUser_sex() == 1 ? '男' : '女');
						m.put("user_name", user.getUser_name());
						m.put("beginTime", user.getExamplan().getBeginTime());
						m.put("examPaperId", user.getExamhistoryinfo().getExamPaperId());
						m.put("examScore", user.getExamhistoryinfo().getExamScore());
						m.put("user_home", user.getUser_home());
						lis.add(m);
					}
					map.put("count",lis.size());
					map.put("data", lis);
				}else {
					String[] split = ep.getCankaoboys().split(","); 
					List<Integer> listInteger=new ArrayList<Integer>(); 
					for (String s : split) {
						Map<String, Integer> m=new HashMap<String, Integer>();
						m.put("studentId", Integer.parseInt(s));
						m.put("examPaperId", examPaperId);
						ExamHistoryInfo xhi = examHistoryPaperService.getUserCheck(m);
						if(xhi == null) {
							ExamHistoryInfo ex=new ExamHistoryInfo(); 
							ex.setStudentId(Integer.parseInt(s));
							ex.setExamPaperId(examPaperId);
							ex.setExamScore(0); 
							examHistoryPaperService.save(ex);
						}
						listInteger.add(Integer.parseInt(s));
					}
					List<Users> li = userServicePan.getAchievementIn(listInteger);
					List<Map<String, Object>> lis=new ArrayList<Map<String,Object>>();
					for (Users user : li) {
						Map<String, Object> m=new HashMap<String, Object>();
						m.put("user_id", user.getUser_id());
						m.put("user_sex", user.getUser_sex() == 1 ? '男' : '女');
						m.put("user_name", user.getUser_name());
						m.put("beginTime", user.getExamplan().getBeginTime());
						m.put("examPaperId", user.getExamhistoryinfo().getExamPaperId());
						m.put("examScore", user.getExamhistoryinfo().getExamScore());
						m.put("user_home", user.getUser_home());
						lis.add(m);
					}
					map.put("count",lis.size());
					map.put("data", lis);
				}
			}
		}
		map.put("code", 0);
		map.put("msg", "");
		return map;
	}
 }
